package com.duoec.magichour.controller.api;

import com.duoec.magichour.core.annotation.Access;
import com.duoec.magichour.core.exceptions.BusinessException;
import com.duoec.magichour.core.web.BaseApiController;
import com.duoec.magichour.dto.BaseResponse;
import com.duoec.magichour.dto.UserInfo;
import com.duoec.magichour.dto.response.UserWithFollowDto;
import com.duoec.magichour.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户关注接口
 */
@RestController
@RequestMapping("/api/user-follow")
public class UserFollowApiController extends BaseApiController {
    @Autowired
    private UserFollowService userFollowService;

    /**
     * 关注某个用户
     *
     * @param userId 用户ID
     * @return
     */
    @Access
    @RequestMapping(method = RequestMethod.POST, value = "/{userId:\\d+}")
    public BaseResponse<Boolean> follow(@PathVariable long userId) {
        long myUserId = getUserInfo().getId();
        if (userId == myUserId) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "不能关注自己！");
        }
        boolean success = userFollowService.follow(myUserId, userId);
        return BaseResponse.success(success);
    }

    /**
     * 关注的用户列表
     *
     * @param userId   用户ID
     * @param pageNo   当前分页数，1为第一页
     * @param pageSize 每页最大记录数
     * @return
     */
    @Access
    @RequestMapping(method = RequestMethod.GET, value = "/user/{userId:\\d+}")
    public BaseResponse<List<UserWithFollowDto>> followedUserList(
            @PathVariable long userId,
            @RequestParam int pageNo,
            @RequestParam int pageSize
    ) {
        UserInfo userInfo = getUserInfo();
        List<UserWithFollowDto> list = userFollowService.getFollowedUserList(userInfo, userId, pageNo, pageSize);
        return BaseResponse.success(list);
    }

    /**
     * 用户粉丝列表
     *
     * @param userId   用户ID
     * @param pageNo   当前分页数，1为第一页
     * @param pageSize 每页最大记录数
     * @return
     */
    @Access
    @RequestMapping(method = RequestMethod.GET, value = "/fans/{userId:\\d+}")
    public BaseResponse<List<UserWithFollowDto>> fansList(
            @PathVariable long userId,
            @RequestParam int pageNo,
            @RequestParam int pageSize
    ) {
        UserInfo userInfo = getUserInfo();
        List<UserWithFollowDto> list = userFollowService.getFansList(userInfo, userId, pageNo, pageSize);
        return BaseResponse.success(list);
    }
}
